package com.ruoyi.weixin.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.weixin.shop.domain.WxHomeLayout;
import com.ruoyi.weixin.shop.domain.vo.WxHomeLayoutAddBo;
import com.ruoyi.weixin.shop.domain.vo.WxHomeLayoutEditBo;
import com.ruoyi.weixin.shop.domain.vo.WxHomeLayoutQueryBo;
import com.ruoyi.weixin.shop.domain.vo.WxHomeLayoutVo;
import com.ruoyi.weixin.shop.mapper.WxHomeLayoutMapper;
import com.ruoyi.weixin.shop.service.IWxHomeLayoutService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商城首页分类Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-15
 */
@Service
public class WxHomeLayoutServiceImpl extends ServiceImpl<WxHomeLayoutMapper, WxHomeLayout> implements IWxHomeLayoutService {

	@Override
	public WxHomeLayoutVo queryById(Long id){
		return getVoById(id, WxHomeLayoutVo.class);
	}

	@Override
	public List<WxHomeLayoutVo> queryList(WxHomeLayoutQueryBo bo) {
		return listVo(buildQueryWrapper(bo), WxHomeLayoutVo.class);
	}

	private LambdaQueryWrapper<WxHomeLayout> buildQueryWrapper(WxHomeLayoutQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<WxHomeLayout> lqw = Wrappers.lambdaQuery();
		lqw.eq(StrUtil.isNotBlank(bo.getCategory()), WxHomeLayout::getCategory, bo.getCategory());
		lqw.eq(StrUtil.isNotBlank(bo.getContent()), WxHomeLayout::getContent, bo.getContent());
		lqw.eq(bo.getDecorateId() != null, WxHomeLayout::getDecorateId, bo.getDecorateId());
		lqw.like(StrUtil.isNotBlank(bo.getName()), WxHomeLayout::getName, bo.getName());
		lqw.eq(StrUtil.isNotBlank(bo.getType()), WxHomeLayout::getType, bo.getType());
		lqw.orderByAsc(WxHomeLayout::getParentId);
		lqw.orderByAsc(WxHomeLayout::getSort);
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(WxHomeLayoutAddBo bo) {
		WxHomeLayout add = BeanUtil.toBean(bo, WxHomeLayout.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(WxHomeLayoutEditBo bo) {
		WxHomeLayout update = BeanUtil.toBean(bo, WxHomeLayout.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(WxHomeLayout entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
